import '../public/index'
import url from '../data/url'
import request from '../data/request'
import {header} from '../data/config'
import store from 'store'
import {getUrlParam, previewImg, imgPreviewModal} from '../data/util'

const base = url.base,
  getBuildingDetail = request.getBuildingDetail,
  getServiceType = request.getServiceType

let data = new Vue({
  el: '#data',
  data: () => ({
    developerName: '',
    idStr: '',
    propertyName: '',
    province: '',
    city: '',
    provinceStr: '',
    cityStr: '',
    propertyAddress: '',
    serviceType: '',
    changePosPay: '',
    serviceTypeCN: '',
    otherServiceType: '',
    otherServiceTypeCN: '',
    channel: '',
    picName: '',
    picMobile: '',
    broker: '',
    rawAddTime: '',
    posSignature: '',
    showBankInfo: '',
    ylTicketCount: '',
    propertyArea: '',
    loginName: '',
    cashierNo: '',
    cashierMobile: '',
    cashierEnpm: '',
    path: '',
    logoPath: '',
    isLock: 0,
    isSignature: 0,
    lockImgPreview: '',
    cancelDeposit: 0,
    refundReceiveSMSPhone: '',
    agreement: null
  }),
  mounted: function () {

    let that = this,
      id = getUrlParam('id'),
      loading

    if (!id) {
      layer.msg('未获取到id,将跳转到登录页面', {
        icon: 5
      })

      setTimeout(() => {
        window.location = './index.html'
      }, 1000)

      return
    }

    loading = layer.msg('初始化数据中...', {
      icon: 16,
      time: 0
    })

    Promise
      .all([this.getBuildingDetail(), this.getServiceTypeList()])
      .then((res) => {

        console.log(res[0].object.serviceType);

        $.extend(that.$data, res[0].object, {
          serviceTypeCN: _.find(res[1].object, (num, key) => (key == res[0].object.serviceType)),
          lockImgPreview: res[0].object.lockImg ? previewImg(res[0].object.lockImg, {'process': 'image/resize,w_350'}) : ''
        })

        layer.close(loading)

      })
      .catch((error) => {
        console.log(error)

        layer.close(loading)
        layer.msg(error || '初始化数据失败', {
          icon: 5
        })
      })
  },
  methods: {
    // 获取楼盘详情
    getBuildingDetail: () => (
      new Promise((resolve, reject) => {

        $.ajax({
          url: base + getBuildingDetail,
          type: 'post',
          headers: header,
          data: {
            id: getUrlParam('id')
          }
        })
          .done(function (res) {
            if (res.code === 0) {
              resolve(res)
            }
            reject(res.message)
          })
          .fail(function () {
            reject()
          })
          .always(function () {
          })

      })
    ),
    // 获取服务类型列表
    getServiceTypeList: () => (
      new Promise((resolve, reject) => {

        $.ajax({
          type: 'POST',
          url: base + getServiceType,
          headers: header,
          data: {
            oneServiceType: ''
          }
        })
          .done(function (res) {
            if (res.code === 0) {
              resolve(res)
            }
            reject(res.message)
          })
          .fail(function () {
            console.log("error")
            reject()
          })
          .always(function () {
          })

      })

    ),
    imgPreviewModal: imgPreviewModal
  }
})